<?php

require_once ('common.php');
$q = trim(stripslashes(htmlspecialchars($_REQUEST['q'])));
if (!empty($q))
{
    $res = $db->query("SELECT t.*, m.realname FROM bbs_topics t INNER JOIN bbs_members m ON m.uid = t.topic_poster WHERE t.topic_title LIKE '%{$q}%' OR t.topic_text LIKE '%{$q}%'");
    if (empty($res))
        $error = '<div class="error">ไม่พบสิ่งที่ค้นหา</div>';
    else
    {
        foreach ($res as $r)
        {
            $r['topic_time'] = getStrTime($r['topic_time']);
            $r['topic_title_raw'] = $r['topic_title'];
            $r['topic_title'] = str_replace($q, '<span class="hightlight">' . $q . '</span>', limit_string($r['topic_title'], 60));
            $topics[] = $r;
        }
    }
}
$pagetitle = 'ค้นหา';
$smarty->assign('pageid', 'search');
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('header.html');
$smarty->assign('error', $error);
$smarty->assign('q', $q);
$smarty->assign('count', count($topics));
$smarty->assign('topics', $topics);
$smarty->display('search.html');
require_once ('sidebar.php');
require_once ('footer.php');

?>